﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ModelPredictPro.General {
    public class Variable {
        int _index;
        public int Index {
            get { return _index; }
            set {
                if (value < 1) throw new ArgumentOutOfRangeException("变量的索引必须为正整数");
                _index = value;
            }
        }
        public double Value {
            get;
            set;
        }

        public Variable(int index, double value) {
            this.Index = index;
            this.Value = value;
        }

        #region static

        public static Variable[] ParseFromUnindexedValueString(string input) {
            string[] strArray = input.Split(default(char[]), StringSplitOptions.RemoveEmptyEntries);
            double[] valueArray = new double[strArray.Length];
            for (int i = 0; i < strArray.Length; i++) {
                double value = 0;
                if (double.TryParse(strArray[i], out value)) {
                    valueArray[i] = value;
                }
                else throw new ArgumentException(string.Format("传入ParseFromUnindexedValueString的参数input : {0} 不是合法的double型数值集合.", input));
            }
            Variable[] rst = new Variable[valueArray.Length];
            for (int i = 0; i < rst.Length; i++) {
                rst[i] = new Variable(i + 1, valueArray[i]);
            }
            return rst;
        }

        public static Variable[] ParseFromIndexedValueString(string input) {
            string[] strArray = input.Split(default(char[]), StringSplitOptions.RemoveEmptyEntries);
            int[] valueArray1 = new int[strArray.Length];
            double[] valueArray2 = new double[strArray.Length];
            Variable[] rst = new Variable[strArray.Length];
            char[] ss_separte = new char[] { ':' };
            for (int i = 0; i < strArray.Length; i++) {//Console.WriteLine(strArray[i]); 
                //int value1 = 0; double value2 = 0;       
                string[] str = strArray[i].Split(ss_separte);
                valueArray1[i] = int.Parse(str[0]);
                valueArray2[i] = double.Parse(str[1]);
                Console.WriteLine("{0}:{1}", str[0], str[1]);
                //  if (int.TryParse(str[0], out value1)&&double.TryParse(str[1],out value2)) {
                //    valueArray1[i] = value1;
                //    valueArray2[i] = value2;       
                //  }
                //  else throw new ArgumentException(string.Format("传入ParseFromUnindexedValueString的参数input : {0} 不是合法的double型数值集合.", input));
            }

            for (int i = 0; i < rst.Length; i++) {
                rst[i] = new Variable(valueArray1[i], valueArray2[i]);
            }
            return rst;
        }
        ///<summary>重置所有变量的索引，适用于变量合并时</summary>
        public static void ResetIndex(Variable[] variables) {
            for (int i = 0; i < variables.Length; i++) {
                variables[i].Index = i + 1;
            }
        }
        ///<summary>合并两组变量，内部重置索引</summary>
        public static Variable[] Union(Variable[] a, Variable[] b) {
            Variable[] ab = new Variable[a.Length + b.Length];
            int index;
            double value;
            for (int i = 0; i < ab.Length; i++) {
                if (i < a.Length) {
                    index = i + 1;
                    value = a[i].Value;
                }
                else {
                    index = i + 1;
                    value = b[i - a.Length].Value;
                }
                ab[i] = new Variable(index, value);
            }
            return ab;
        }

        #endregion
    }
}
